import java.io.*;

public class FirstScan {

	
	public FirstScan()
	{
	
	}
	
	public LargeKey CKnown(String plain, String IV, String cipher, int blockSize) 
	{
		LargeKey lKey = new LargeKey();
		byte[] bytesPlain, bytesIV, bytesCipher, bytesTemp;
		int i, j, IVPlainlen;
		char afterXor;
		
		try {
			File fPlain = new File(plain);
			FileInputStream inputPlain = new FileInputStream(fPlain);
			
			File fIV = new File(IV);
			FileInputStream inputIV = new FileInputStream(IV);
			
			File fCipher = new File(cipher);
			FileInputStream inputCipher = new FileInputStream(cipher);
			
					
			
			bytesPlain = new byte[(int)fPlain.length()];
			inputPlain.read(bytesPlain);
			bytesIV = new byte[(int)fIV.length()];
			inputIV.read(bytesIV);
			bytesCipher = new byte[(int)fCipher.length()];
			inputCipher.read(bytesCipher);
			
			IVPlainlen = blockSize + bytesPlain.length;
			if ((IVPlainlen % blockSize) == 0)
				bytesTemp = new byte[IVPlainlen];
			else
				bytesTemp = new byte[IVPlainlen + (blockSize - (IVPlainlen % blockSize))];
			
			
			for(i = 0; i < blockSize; i++)
				bytesTemp[i] = bytesIV[i];
			for(i = 0, j = blockSize; i < bytesPlain.length; i++, j++)
				bytesTemp[j] = bytesPlain[i];
			for(; j < bytesTemp.length; j++)
				bytesTemp[j] = (byte)'0';						

			char ch=0;
			for(i = blockSize; i < bytesTemp.length; i++)
			{
				ch = (char)(bytesCipher[i - blockSize]);
				if ( !(ch>='a' && ch<='z') &&  !(ch>='A' && ch<='Z'))
					continue;
				
				afterXor = (char)(bytesTemp[i] ^ bytesTemp[i - blockSize]);
				if( ((afterXor >= 'a' && afterXor <= 'z') || (afterXor >= 'A' && afterXor <= 'Z')) )
				{
					lKey.SetValue((char)(bytesCipher[i - blockSize]), afterXor, "cipher");
					lKey.SetValue(afterXor, (char)(bytesCipher[i - blockSize]), "plain");
				}
				bytesTemp[i] = bytesCipher[i - blockSize];
			}					
						
			inputPlain.close();
			inputIV.close();			
			inputCipher.close();

		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}	
		return lKey;
	}
}
